<template>
  <div class="room-list-container">
    <div class="room-list-header">
      <h2>房间列表</h2>
    </div>

    <div v-if="rooms && rooms.length" class="room-list">
      <div v-for="room in rooms" :key="room.id" class="room-item">
        <div class="room-info">
          <div class="room-name">{{ room.name }}</div>
          <div class="room-users">{{ room.userCount }}/{{ room.maxUsers }} 人</div>
        </div>
        <div :class="['room-status', { 'is-playing': room.isPlaying }]">{{ room.isPlaying ? '游戏中' : '等待中' }}</div>
        <button @click="joinRoom(room.id)" class="join-btn">加入</button>
      </div>
    </div>

    <div v-else class="empty-state">
      <div class="empty-state-icon">📭</div>
      <p>暂无可用房间，创建一个新房间开始游戏吧！</p>
      <button @click="showCreateRoomDialog = true" class="create-btn">创建房间</button>
    </div>

    <!-- 创建房间弹窗 -->
    <div v-if="showCreateRoomDialog" class="dialog-overlay">
      <div class="dialog-content">
        <h3>创建房间</h3>
        <input
          v-model="roomName"
          placeholder="输入房间名称"
          class="room-name-input"
        >
        <select v-model="maxUsers" class="max-users-select">
          <option value="2">2人</option>
          <option value="4">4人</option>
          <option value="8">8人</option>
        </select>
        <button @click="createRoom" class="create-room-btn">创建</button>
        <button @click="showCreateRoomDialog = false" class="close-btn">关闭</button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { roomApi } from '@/api'
import { useUserStore } from '@/stores/user'

uni.showToast({ title: '进入房间列表', icon: 'none', duration: 1000 })
// 状态管理
const userStore = useUserStore()

// 房间数据
const rooms = ref([])
const showCreateRoomDialog = ref(false)
const roomName = ref('')
const maxUsers = ref('2')

// 获取房间列表
const getRoomList = async () => {
  try {
    const res = await roomApi.getRoomList()
    rooms.value = res.data
  } catch (error) {
    console.error('获取房间列表失败:', error)
    uni.showToast({ title: '获取房间列表失败', icon: 'none' })
  }
}

// 创建房间
const createRoom = async () => {
  if (!roomName.value.trim()) {
    uni.showToast({ title: '请输入房间名称', icon: 'none' })
    return
  }

  try {
    const res = await roomApi.createRoom({
      name: roomName.value,
      maxUsers: parseInt(maxUsers.value)
    })
    uni.showToast({ title: '房间创建成功', icon: 'success' })
    showCreateRoomDialog.value = false
    roomName.value = ''
    // 加入创建的房间
uni.setStorageSync('roomId', res.data.roomId)
uni.navigateTo({ url: '/pages/chat/chat' })
  } catch (error) {
    console.error('创建房间失败:', error)
    uni.showToast({ title: '创建房间失败', icon: 'none' })
  }
}

// 加入房间
const joinRoom = async (roomId) => {
  try {
    await roomApi.joinRoom(roomId)
    uni.setStorageSync('roomId', roomId)
    uni.navigateTo({ url: '/pages/chat/chat' })
  } catch (error) {
    console.error('加入房间失败:', error)
    uni.showToast({ title: '加入房间失败', icon: 'none' })
  }
}

// 组件挂载时
onMounted(() => {
  if (!userStore.isLogin) {
    uni.navigateTo({ url: '/pages/login/login' })
    return
  }

  getRoomList()
  // 定期刷新房间列表
  setInterval(getRoomList, 5000)
})
</script>

<style scoped>
.room-list-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
  min-height: 100vh;
  background-color: #f5f7fa;
}

.room-list-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  padding-bottom: 16px;
  border-bottom: 1px solid #e8e8e8;
}

.room-list-header h2 {
  font-size: 24px;
  font-weight: 600;
  color: #1f2937;
  margin: 0;
}

.create-btn {
  padding: 10px 20px;
  background-color: #4096ff;
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 500;
  transition: all 0.3s ease;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.create-btn:hover {
  background-color: #3182ce;
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
}

.room-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.room-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border: 1px solid #e0e0e0;
  border-radius: 12px;
  background-color: white;
  transition: all 0.3s ease;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.room-item:hover {
  transform: translateY(-3px);
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
  border-color: #e0e0e0;
}

.room-info {
  flex: 1;
}

.room-name {
  font-weight: 600;
  font-size: 18px;
  margin-bottom: 6px;
  color: #1f2937;
}

.room-users {
  font-size: 14px;
  color: #6b7280;
  display: flex;
  align-items: center;
}

.room-users::before {
  content: '👥';
  margin-right: 6px;
}

.room-status.is-playing {
  margin-right: 16px;
  padding: 4px 10px;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 500;
}

.room-status:not(.is-playing) {
  background-color: #ecfdf5;
  color: #10b981;
}

.room-status.is-playing {
  background-color: #fee2e2;
  color: #ef4444;
}

.join-btn {
  padding: 8px 18px;
  background-color: #4096ff;
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 500;
  transition: all 0.2s ease;
}

.join-btn:hover {
  background-color: #3182ce;
}

.join-btn:disabled {
  background-color: #94a3b8;
  cursor: not-allowed;
}

.dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
  backdrop-filter: blur(4px);
  animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.dialog-content {
  background-color: white;
  padding: 28px;
  border-radius: 12px;
  width: 90%;
  max-width: 400px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
  animation: slideUp 0.3s ease;
}

@keyframes slideUp {
  from { transform: translateY(20px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

.dialog-content h3 {
  margin-top: 0;
  margin-bottom: 20px;
  font-size: 18px;
  font-weight: 600;
  color: #1f2937;
  text-align: center;
}

.room-name-input,
.max-users-select {
  width: 100%;
  padding: 12px 16px;
  margin-bottom: 16px;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  font-size: 14px;
  transition: border-color 0.2s ease;
}

.room-name-input:focus,
.max-users-select:focus {
  outline: none;
  border-color: #4096ff;
  box-shadow: 0 0 0 3px rgba(64, 150, 255, 0.1);
}

.create-room-btn,
.close-btn {
  width: 100%;
  padding: 12px;
  border-radius: 6px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  border: none;
  font-size: 14px;
}

.create-room-btn {
  background-color: #4096ff;
  color: white;
  margin-bottom: 10px;
}

.create-room-btn:hover {
  background-color: #3182ce;
}

.close-btn {
  background-color: #f3f4f6;
  color: #4b5563;
}

.close-btn:hover {
  background-color: #e5e7eb;
}

/* Empty state styles */
.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: #6b7280;
}

.empty-state p {
  margin-bottom: 20px;
  font-size: 16px;
}

.empty-state-icon {
  font-size: 48px;
  margin-bottom: 16px;
  color: #d1d5db;
}

.dialog-content {
  width: 90%;
  max-width: 500px;
}

.room-name-input {
  width: 100%;
  padding: 12px;
  border: 1px solid #e0e0e0;
  border-radius: 4px;
  margin-bottom: 16px;
}

.max-users-select {
  width: 100%;
  padding: 12px;
  border: 1px solid #e0e0e0;
  border-radius: 4px;
  margin-bottom: 16px;
}

.create-room-btn {
  width: 100%;
  padding: 12px;
  background-color: #1890ff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  margin-bottom: 16px;
}

.close-btn {
  width: 100%;
  padding: 12px;
  background-color: #f5f5f5;
  color: #333333;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}
</style>